<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="学生id" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="辅导员id" prop="counsellorId">
        <el-input
          v-model="queryParams.counsellorId"
          placeholder="请输入辅导员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="辅导员名称" prop="counsellorName">
        <el-input v-model="queryParams.counsellorName" placeholder="请输入辅导员名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker type="datetimerange" v-model="beginTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable></el-date-picker>
        </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
          <el-date-picker type="datetimerange" v-model="endTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable></el-date-picker>
        </el-form-item>
      <el-form-item label="批准人名称" prop="ratifyName">
        <el-input v-model="queryParams.ratifyName" placeholder="请输入批准人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['edu:leave:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaveList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="主键id" align="center" prop="askLeaveId" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="辅导员" align="center" prop="counsellorName" />
      <el-table-column label="请假类型" align="center" prop="typeName" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.beginTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假原因" align="center" prop="reason" />

      <el-table-column label="批准时间" align="center" prop="ratifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.ratifyTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="批准人名称" align="center" prop="ratifyName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:leave:edit']"
          >修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleShowDetail(scope.row)">查看</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:leave:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 查看和撤销对话框 -->
    <el-dialog title="请假申请" :visible.sync="applyDialogFormVisible" width="500px" append-to-body center :before-close="handleClose" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="学生姓名" :disabled="dialogType" />
        </el-form-item>
        <el-form-item label="辅导员" prop="counsellorName">
          <el-input v-model="form.counsellorName" placeholder="辅导员姓名" :disabled="dialogType" />
        </el-form-item>
        <el-form-item label="请假类型">
          <el-select v-model="form.type" placeholder="请选择请假类型" :disabled="dialogType">
            <el-option v-for="dict in dict.type.edu_leave_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" v-if="dialogType">
          <el-select v-model="form.status" placeholder="状态" :disabled="dialogType">
            <el-option v-for="dict in dict.type.edu_leave_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请假时间" prop="leaveTime">
          <el-date-picker type="datetimerange" v-model="leaveTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable :disabled="dialogType"></el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因">
          <el-input type="textarea" v-model="form.reason" placeholder="请输入请假原因" :disabled="dialogType"></el-input>
        </el-form-item>

        <el-form-item v-if="form.status == 1">
          <el-button type="primary" @click="handleAppreve(form.askLeaveId,2)">同意</el-button>
          <el-button type="primary" @click="handleAppreve(form.askLeaveId,3)">拒绝</el-button>
          <!-- <el-button type="primary" @click="submitForm" v-if="!dialogType">提交申请</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { apply, listCounsellor, approve, listLeave, getLeave, delLeave, addLeave, updateLeave } from "@/api/edu/leave";

export default {
  name: "Leave",
  dicts: ['edu_leave_type', 'edu_leave_status'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生请假表格数据
      leaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        counsellorName: null,
        beginTime: null,
        beginStartTime:null,
        beginStopTime:null,
        endTime: null,
        endStartTime: null,
        endStopTime: null,
        reason: null,
        status: null,
        ratifyTime: null,
        ratifyName: null,
        type: null,
      },
      beginTime:null,//开始时间
      endTime:null, //结束时间
      // 表单参数
      form: {
        beginTime: '',
        endTime: '',
        reason: '',
        type: null,
      },
      applyDialogFormVisible: false,
      leaveTime: '',
      // 表单校验
      rules: {
      },
      dialogType: false
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询学生请假列表 */
    getList () {
      this.loading = true;
      listLeave(this.queryParams).then(response => {
        this.leaveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //审批
    handleAppreve (askLeaveId, status) {
      approve({ askLeaveId, status }).then(res => {
        this.$modal.msgSuccess("审批成功")
        this.getList()
        this.applyDialogFormVisible = false
      })
    },
    handleClose () {
      this.resetForm()
      this.applyDialogFormVisible = false
    },
    resetForm () {
      this.form = {
        beginTime: '',
        endTime: '',
        reason: '',
        type: null,
      }

      this.leaveTime = ''
    },
    //查看
    handleShowDetail (row) {
      this.dialogType = true
      this.applyDialogFormVisible = true;
      this.form = row
      this.form.type = row.type + ''
      this.form.status = row.status + ''
      this.leaveTime = [this.form.beginTime, this.form.endTime]
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        askLeaveId: null,
        studentId: null,
        studentName: null,
        counsellorId: null,
        counsellorName: null,
        beginTime: null,
        endTime: null,
        reason: null,
        status: null,
        ratifyTime: null,
        ratifyName: null,
        type: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      if(this.beginTime){
        this.queryParams.beginStartTime = this.beginTime[0]
        this.queryParams.beginStopTime = this.beginTime[1]
      }else{
        this.queryParams.beginStartTime = null
        this.queryParams.beginStopTime = null
      }
      if(this.endTime){
        this.queryParams.endStartTime = this.endTime[0]
        this.queryParams.endStopTime = this.endTime[1]
      }else{
        this.queryParams.endStartTime = null
        this.queryParams.endStopTime = null
      }
      
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      // this.resetForm("queryForm");
      this.resetQueryParams()
      this.beginTime = null
      this.endTime = null
      this.handleQuery();
    },
    resetQueryParams(){
        this.queryParams = {
          pageNum: 1,
        pageSize: 10,
        studentName: null,
        counsellorName: null,
        beginTime: null,
        beginStartTime:null,
        beginStopTime:null,
        endTime: null,
        endStartTime: null,
        endStopTime: null,
        reason: null,
        status: null,
        ratifyTime: null,
        ratifyName: null,
        type: null,
        }
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.askLeaveId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加学生请假";
    },
    // //查询详情操作
    // handleShowDetail () {
    //   this.reset();
    //   const askLeaveId = row.askLeaveId || this.ids
    //   getLeave(askLeaveId).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "请假详情";
    //   });
    // },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const askLeaveId = row.askLeaveId || this.ids
      getLeave(askLeaveId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生请假";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.askLeaveId != null) {
            updateLeave(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeave(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const askLeaveIds = row.askLeaveId || this.ids;
      this.$modal.confirm('是否确认删除学生请假编号为"' + askLeaveIds + '"的数据项？').then(function () {
        return delLeave(askLeaveIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('edu/leave/export', {
        ...this.queryParams
      }, `leave_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
